home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 299_01 / mel_test.c < prev    next >
C/C++ Source or Header  |  1989-12-28  |  10KB  |  315 lines

  1. /*
  2. ---------------------------------------------------------------------------
  3. filename: mel_test.c
  4.  
  5. this file contains example code for MEL - a metalanguage data processor.
  6. it may be used as a driver by customizing modules: 1.1, 2.1, 3.0, and 4.0.
  7.  
  8. functions contained in this file:
  9.  
  10. module #   name
  11. --------   ------------------------------
  12. 0.0       main
  13. 1.0       initialize
  14. 1.1       perform_global_initializations
  15. 2.0       input_MEL_data
  16. 2.1       translate_input_datum
  17. 3.0       solve_problem
  18. 4.0       output_MEL_data
  19. ---------------------------------------------------------------------------
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <time.h>
  25.  
  26. /* MEL interface: */
  27. #define MEL_INPUT
  28. #define MEL_OUTPUT
  29. #include "mel.h"
  30.  
  31. void main(int, char *[]);
  32. void initialize(void);
  33. void perform_global_initializations(void);
  34. void input_MEL_data(void);
  35. void translate_input_datum(void);
  36. void solve_problem(void);
  37. void output_MEL_data(void);
  38.  
  39. /* i/o/err file data: */
  40. FILE *input_file_handle;
  41. FILE *output_file_handle;
  42. FILE *error_file_handle;
  43. #define MAX_FILENAME_LEN 80
  44. char input_filename[MAX_FILENAME_LEN+1] = "stdin";
  45. char output_filename[MAX_FILENAME_LEN+1] = "stdout";
  46. char error_filename[MAX_FILENAME_LEN+1] = "stderr";
  47.  
  48. /* other program global data (specific to this example): */
  49. #define MAX_STRING_LEN 80
  50. char program[MAX_STRING_LEN];
  51. char date[MAX_STRING_LEN+1];
  52. char label[MAX_STRING_LEN+1];
  53. char output_format[MAX_STRING_LEN+1];
  54. int verbose_flag;
  55. int message_code;
  56. char message_text[MAX_STRING_LEN+1];
  57. int label_flag,
  58.     output_format_flag,
  59.     message_code_flag,
  60.     message_text_flag;
  61.     /* has any of the above data been changed with new input data? */
  62.  
  63. /*
  64. ---------------------------------------------------------------------------
  65. module 0.0
  66. ---------------------------------------------------------------------------
  67. */
  68. void main(
  69. int argc,    /* command line args specify file i/o (see below) */
  70. char *argv[])
  71. {
  72.  
  73.     /* first, see if i/o files are other than the default: */
  74.     if (argc > 1) strncpy(input_filename, argv[1], MAX_FILENAME_LEN);
  75.     if (argc > 2) strncpy(output_filename, argv[2], MAX_FILENAME_LEN);
  76.     if (argc > 3) strncpy(error_filename, argv[3], MAX_FILENAME_LEN);
  77.  
  78.     initialize();
  79.     /* global program initializations. */
  80.  
  81.     input_MEL_data();
  82.     /* a standard meta-language input format is being used.
  83.        get input for problem to be solved. */
  84.  
  85.     solve_problem();
  86.     /* generate output from input. */
  87.  
  88.     output_MEL_data();
  89.     /* standard meta-language output is used.  write results in this
  90.        format to file */
  91. }
  92.  
  93. /*
  94. ---------------------------------------------------------------------------
  95. module 1.0
  96. ---------------------------------------------------------------------------
  97. */
  98. void initialize()
  99. {
  100.     struct tm *newtime;      /* so we can write current time/date */
  101.     time_t aclock;
  102.     char *time_msg;
  103.  
  104.     time(&aclock);            /* get time in seconds */
  105.     newtime = localtime(&aclock);   /* convert time to struct tm */
  106.     time_msg = asctime(newtime);    /* pointer to time as ascii string */
  107.     time_msg[24] = '\0';        /* get rid of \n char */
  108.     strcpy(date, time_msg);
  109.  
  110.     /* open i/o/err files: */
  111.  
  112.     if (strcmp(input_filename, "stdin") == 0)
  113.     input_file_handle = stdin;
  114.     else if ((input_file_handle = fopen(input_filename, "r")) == NULL) {
  115.     printf("Can't open file <%s>.\n", input_filename);
  116.     exit(0);
  117.     };
  118.     if (strcmp(output_filename, "stdout") == 0)
  119.     output_file_handle = stdout;
  120.     else if ((output_file_handle = fopen(output_filename, "w")) == NULL) {
  121.     printf("Can't open file <%s>.\n", output_filename);
  122.     exit(0);
  123.     };
  124.     if (strcmp(error_filename, "stderr") == 0)
  125.     error_file_handle = stderr;
  126.     else if ((error_file_handle = fopen(error_filename, "w")) == NULL) {
  127.     printf("Can't open file <%s>.\n", error_filename);
  128.     exit(0);
  129.     };
  130.  
  131.     perform_global_initializations();
  132.     /* also perform other application specific initializations. */
  133. }
  134.  
  135. /*
  136. ---------------------------------------------------------------------------
  137. module 1.1
  138.  
  139. perform global data initializations that are application specific.
  140. ---------------------------------------------------------------------------
  141. */
  142. void perform_global_initializations()
  143. {
  144.     strcpy(program, "TEST_MEL - a driver program for debugging MEL");
  145.     label_flag = 0;
  146.     output_format_flag = 0;
  147.     verbose_flag = 1;
  148.     message_code_flag = 0;
  149.     message_text_flag = 0;
  150. }
  151.  
  152. /*
  153. ---------------------------------------------------------------------------
  154. module 2.0
  155. ---------------------------------------------------------------------------
  156. */
  157. void input_MEL_data()
  158. {
  159.     while (!meli_file(input_file_handle)) {
  160.     /* continue to read descriptors until there are no more. */
  161.  
  162.     if (!strcmp("end_of_data", meli_descrip_type())) break;
  163.         /* break if no more data left. */
  164.  
  165.     translate_input_datum();
  166.         /* save the information read and go read another descriptor. */
  167.     }
  168.  
  169.     switch (mel_err.type) {
  170.     /* find out why we left the above loop. */
  171.  
  172.     case (mel_no_err):
  173.         /* must have encountered an end_of_data descriptor. */
  174.     case (mel_end_of_data_err):
  175.         /* this is not really an error, it just means no more data
  176.            to follow. */
  177.         break;
  178.  
  179.     default:
  180.         /* some sort of read error must have occured.
  181.            tell user about error and exit program. */
  182.  
  183.         fprintf(error_file_handle,
  184.         "\nUnrecoverable error in MEL data input file.");
  185.         if (mel_err.start_line)
  186.         fprintf(error_file_handle,
  187.             "\n(Look at lines on or after %d, through %d.)",
  188.             mel_err.start_line, mel_err.end_line);
  189.         fprintf(error_file_handle, "\nData = <%s>",
  190.         meli_descriptor_string);
  191.         fprintf(error_file_handle, "\nError number %d, %s.",
  192.         (int)mel_err.type, mel_err_msg[(int)mel_err.type]);
  193.         fprintf(error_file_handle, "\nAdd info: <%s>.", mel_err.msg);
  194.         exit(0);
  195.     }
  196. }
  197.  
  198. /*
  199. ---------------------------------------------------------------------------
  200. module 2.1
  201.  
  202. transfer input data from meli_datum data structure into global program
  203. variables specific to this application.
  204. ---------------------------------------------------------------------------
  205. */
  206. void translate_input_datum()
  207. {
  208.     union meli_param_data data;        /* data to be translated: */
  209.     char units[MELI_UNITS_STR_LEN+1];
  210.     int array_len;
  211.     int unknown_flag;
  212.  
  213.     char *descrip_type;    /* pointer to meli_datum.descrip_type */
  214.  
  215.     descrip_type = meli_descrip_type();    /* what has just been read? */
  216.  
  217.     if (!strcmp("program_data", descrip_type)) {
  218.  
  219.     if (label_flag = !meli_data("label", &data, units,
  220.         &array_len, &unknown_flag))
  221.         strcpy(label, data.string);
  222.  
  223.     } else if (!strcmp("program_options", descrip_type)) {
  224.  
  225.     if (output_format_flag = !meli_data("output_format",
  226.         &data, units, &array_len, &unknown_flag)) {
  227.         strcpy(output_format, data.string);
  228.         if (!strcmp(output_format, "terse")) verbose_flag = 0;
  229.     };
  230.  
  231.     } else if (!strcmp("message", descrip_type)) {
  232.  
  233.     if (message_code_flag = !meli_data("code",
  234.         &data, units, &array_len, &unknown_flag))
  235.         message_code = data.integer;
  236.     if (message_text_flag = !meli_data("text",
  237.         &data, units, &array_len, &unknown_flag))
  238.         strcpy(message_text, data.string);
  239.     };
  240. }
  241.  
  242. /*
  243. ---------------------------------------------------------------------------
  244. module 3.0
  245.  
  246. solve problem, that is, generate output data from input data.
  247. ---------------------------------------------------------------------------
  248. */
  249. void solve_problem()
  250. {
  251.     /* no code required for this example. */
  252. }
  253.  
  254. /*
  255. ---------------------------------------------------------------------------
  256. module 4.0
  257.  
  258. transfer application specific output data into melo_datum data structure(s)
  259. and output to file.
  260. ---------------------------------------------------------------------------
  261. */
  262. void output_MEL_data()
  263. {
  264.     union melo_param_data data;        /* data to be translated: */
  265.     char units[MAX_STRING_LEN+1];
  266.     int array_len = 0;
  267.     int unknown_flag = 0;
  268.  
  269.